//
//  GameManager.m
//  Coffee
//
//  Created by sabhanayagam b on 4/29/11.
//  Copyright 2011 Cognizant. All rights reserved.
//

#import "GameManager.h"

static GameManager* _sharedGameInfo = nil;

@implementation GameManager

@synthesize isGamePause;


+(GameManager*) sharedGameInfo
{
	@synchronized([GameManager class])
	{
		if (!_sharedGameInfo)
			[[self alloc] init];
		
		return _sharedGameInfo;
	}
	
	return nil;
}

+(id)alloc
{
	@synchronized([GameManager class])
	{
		NSAssert(_sharedGameInfo == nil, @"Attempted to allocate a second instance of a singleton.");
		_sharedGameInfo = [super alloc];
		return _sharedGameInfo;
	}
	
	return nil;
}

-(id)init {
	self = [super init];
	if (self != nil) {
		// initialize stuff here
		isGamePause = NO;
	}
	
	return self;
}

-(void) switchScene:(TargetScenes)targetScene
{
	LoadingScene* scene = [LoadingScene sceneWithTargetScene:targetScene];
	[[CCDirector sharedDirector] replaceScene:scene];
	
}
+(CGSize) size
{
	// ask director the the window size
	return [[CCDirector sharedDirector] winSize];
	
}


+(CGPoint) locationFromTouch:(UITouch*)touch
{
	CGPoint touchLocation = [touch locationInView: [touch view]];
	return [[CCDirector sharedDirector] convertToGL:touchLocation];
}

+(CGPoint) locationFromTouches:(NSSet*)touches
{
	return [self locationFromTouch:[touches anyObject]];
}
-(void) pauseGame{
	
	isGamePause = YES;
	[[CCDirector sharedDirector] pause];
	
}
-(void) resumeGame
{
	isGamePause = NO;
	[[CCDirector sharedDirector] resume];

}
-(void) restartGame{

	[[CCDirector sharedDirector] resume];
	[[GameManager sharedGameInfo] switchScene:TargetMenuScene];
}
-(void) restartLevel{

	[[CCDirector sharedDirector] resume];
	[[GameManager sharedGameInfo] switchScene:TargetGameScene];
}
-(void) dealloc
{
	CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	
	// The Layer will be gone now, to avoid crashes on further access it needs to be nil.
	_sharedGameInfo = nil;
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
